package com.hellogood.yaohuo.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.ConnectionPool;
import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.data.CallDTO;
import com.hellogood.yaohuo.shared.entity.PdDetail;
import com.hellogood.yaohuo.shared.entity.PdForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PdFormDAO extends DBHelper {
	Logger log = Logger.getLogger(this.getClass());

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<PdForm> pdFormList = new ArrayList<PdForm>();
		if (rs != null) {
			while (rs.next()) {
				PdForm pdForm = new PdForm();
				pdForm.setPdBillNo(rs.getString("pdBillNo"));
				pdForm.setPdDate(rs.getString("pdDate"));
				pdForm.setOrgCode(rs.getString("OrgCode"));
				pdForm.setYkCounts(rs.getInt("ykCounts"));
				pdForm.setYkCost(rs.getBigDecimal("ykCost"));
				pdForm.setNetCost(rs.getBigDecimal("ykNetCost"));
				pdForm.setYkTotal(rs.getBigDecimal("ykTotal"));
				pdForm.setStaffCode(rs.getString("StaffCode"));
				pdForm.setStaffName(rs.getString("StaffName"));
				pdForm.setTimeMark(rs.getString("TimeMark"));
				pdForm.setRzDate(rs.getString("RzDate"));
				pdForm.setRemark(rs.getString("Remark"));
				pdFormList.add(pdForm);
			}
		}
		return pdFormList;
	}

	public String getLastPdBillNo(String billNo) {
		String sql = "SELECT top 1 * FROM PdForm WHERE pdbillno LIKE '" + billNo + "%' ORDER BY pdbillno desc";
		System.out.println(sql);
		List<?> list = super.query(sql);
		if (list == null || list.isEmpty()) {
			return null;
		}
		PdForm form = (PdForm) list.get(0);
		return form.getPdBillNo();
	}

	public String createPandian(PdForm form, boolean isModify) {
		String result = "";
		List<String> sqls = new ArrayList<String>();
		if (isModify) {
			String sql = "DELETE FROM PdForm WHERE pdBillNo = '" + form.getPdBillNo() + "'";
			sqls.add(sql);
			sql = "DELETE FROM PdDetail WHERE pdBillNo = '" + form.getPdBillNo() + "'";
			sqls.add(sql);
		}
		String sql = "INSERT INTO PdForm(pdBillNo,pdDate,orgCode,ykcounts,ykcost,yktotal,staffCode,staffName,rzDate,remark,ykNetCost)" + " VALUES('"
				+ form.getPdBillNo() + "',CONVERT(varchar(100), GETDATE(), 23),'" + form.getOrgCode() + "'," + form.getYkCounts() + ","
				+ form.getYkCost() + "," + form.getYkTotal() + ",'" + form.getStaffCode() + "','" + form.getStaffName() + "','" + form.getRzDate()
				+ "','" + form.getRemark() + "'," + form.getNetCost() + ")";
		sqls.add(sql);

		List<PdDetail> list = form.getList();
		if (list != null && (!list.isEmpty())) {
			for (PdDetail d : list) {
				sql = "INSERT INTO PdDetail(pdBillNo,pluCode,pluName,barCode,bra,color,csize,zmCounts,sjCounts,ykCounts,jPrice,netPrice,"
						+ "price,zmCost,zmNetCost,zmTotal,sjCost,sjNetCost,sjTotal,ykCost,ykNetCost,ykTotal,rzDate,remark,orgCode,depCode,depName) VALUES('"
						+ d.getPdBillNo()
						+ "','"
						+ d.getPluCode()
						+ "','"
						+ d.getPluName()
						+ "','"
						+ d.getBarCode()
						+ "','"
						+ d.getBra()
						+ "','"
						+ d.getColor()
						+ "','"
						+ d.getSize()
						+ "',"
						+ d.getZmCounts()
						+ ","
						+ d.getSjCounts()
						+ ","
						+ d.getYkCounts()
						+ ","
						+ d.getjPrice()
						+ ","
						+ d.getjPrice()
						+ ","
						+ d.getPrice()
						+ ","
						+ d.getZmCost()
						+ ","
						+ d.getZmCost()
						+ ","
						+ d.getZmTotal()
						+ ","
						+ d.getSjCost()
						+ ","
						+ d.getSjCost()
						+ ","
						+ d.getSjTotal()
						+ ","
						+ d.getYkCost()
						+ ","
						+ d.getYkCost()
						+ ","
						+ d.getYkTotal()
						+ ",'"
						+ d.getRzDate()
						+ "','"
						+ d.getRemark()
						+ "','"
						+ d.getOrgCode()
						+ "','"
						+ d.getDepCode()
						+ "','"
						+ d.getDepName() + "')";
				sqls.add(sql);
			}
		}
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<PdForm> searchPdForm(String orgCode, String startDate, String endDate, String rz) {
		String sql = "SELECT * FROM PdForm WHERE OrgCode = ? AND cast(pdDate as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,?),120) and cast(pdDate as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,?),120)";
		if (rz.equals("已入账")) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals("未入账")) {
			sql = sql + " AND rzDate = ''";
		}
		Object[] args = new Object[] { orgCode, startDate, endDate };
		List<?> list = super.query(sql, args);
		return (List<PdForm>) list;
	}

	@SuppressWarnings("unchecked")
	public List<PdForm> searchPdFormByQuyu(String quyuCode, String startDate, String endDate, String rz) {
		String userCode = MainFrame.currentUser.getStaffCode();
		String orgs = "";
		if (quyuCode == null) {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUser(userCode);
		} else {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUserQuyu(userCode, quyuCode);
		}

		String sql = "SELECT * FROM PdForm WHERE OrgCode IN (" + orgs + ") AND cast(pdDate as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,'"
				+ startDate + "'),120) and cast(pdDate as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + endDate + "'),120)";
		if (rz.equals("已入账")) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals("未入账")) {
			sql = sql + " AND rzDate = ''";
		}
		List<?> list = super.query(sql);
		return (List<PdForm>) list;
	}

	public PdForm getFormByBillNo(String billNo) {
		String sql = "SELECT * FROM PdForm WHERE pdBillNo = ?";
		Object[] args = new Object[] { billNo };
		List<?> list = super.query(sql, args);
		if (list != null && (!list.isEmpty())) {
			return (PdForm) list.get(0);
		}
		return null;
	}

	public String deletePandian(String billNo) {
		String result = "";
		List<String> sqls = new ArrayList<String>();

		String sql = "DELETE FROM PdForm WHERE pdBillNo = '" + billNo + "'";
		sqls.add(sql);
		sql = "DELETE FROM PdDetail WHERE pdBillNo = '" + billNo + "'";
		sqls.add(sql);
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return result;
	}

	public void rz(String bill) throws DatabaseException {
		CallDTO dto = new CallDTO();
		String sql = "UPDATE pdForm SET rzDate WHERE PdBillNo = '" + bill + "'";
		String call = "{CALL pdrz(?,?)}";
		dto.setBill(bill);
		dto.setCall(call);
		dto.setSql(sql);
		dto.setHxdbName(ConnectionPool.hxDbName);
		super.callProcedure(dto);
	}
}
